package v1.chapter12.fileChooser;

import java.io.File; 

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class ImageViewerFrame extends JFrame {
	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 400;
	private JLabel label;
	private JFileChooser chooser;

	public ImageViewerFrame() {
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
 
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu menu = new JMenu("File");
		menuBar.add(menu);

		JMenuItem openItem = new JMenuItem("Open");
		menu.add(openItem);
		openItem.addActionListener(event -> {
			chooser.setCurrentDirectory(new File(".")); 
			int result = chooser.showOpenDialog(ImageViewerFrame.this); 
			if (result == JFileChooser.APPROVE_OPTION) {
				String name = chooser.getSelectedFile().getPath();
				label.setIcon(new ImageIcon(name));
				pack();
			}
		});

		JMenuItem exitItem = new JMenuItem("Exit");
		menu.add(exitItem);
		exitItem.addActionListener(event -> System.exit(0));
 
		label = new JLabel();
		add(label);
 
		chooser = new JFileChooser(); 
		FileFilter filter = new FileNameExtensionFilter("Image files", "jpg", "jpeg", "gif","png");
		chooser.setFileFilter(filter); 
		chooser.setAccessory(new ImagePreviewer(chooser));

		chooser.setFileView(new FileIconView(filter, new ImageIcon("palette.gif")));
	}
}
